package com.matt.service.impl;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;

import com.matt.domain.Leads;
import com.matt.domain.LeadsHistory;
import com.matt.domain.LeadsUser;

import com.matt.foundation.annotation.OptsTrace;
import com.matt.foundation.model.ErrorCode;

import com.matt.foundation.utils.DateUtil;
import com.matt.remoteCall.SsoCallService;

import com.matt.repository.LeadsHistoryRepository;
import com.matt.repository.LeadsRepository;
import com.matt.repository.LeadsUserRepository;

import com.matt.service.LeadsService;
import com.matt.service.LeadsUserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import java.util.*;

import static com.matt.foundation.constant.OperateConstant.OPERATE_CREATE;
import static com.matt.foundation.constant.OperateConstant.OPERATE_FOLLOW;


/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/6/13
 * *******************************
 */
@Service
@Transactional
public class LeadsUserServiceImpl implements LeadsUserService {
    @Autowired
    private LeadsUserRepository leadsUserRepository;
    @Autowired
    private SsoCallService ssoCallService;
    @Autowired
    private LeadsRepository leadsRepository;
    @Autowired
    private LeadsHistoryRepository leadsHistoryRepository;

    @Override
    public Object save(LeadsUser leadsUser, String ticket) {
        StaffDTO modifyStaff = ssoCallService.getStaffDTO(ticket);


        leadsUser.setAreaId(modifyStaff.getAreaId());
//        LeadsUser exist = leadsUserRepository.findTopByChildBigNameAndMobileAndAreaIdAndDeletedFalse(leadsUser.getChildBigName(), leadsUser.getMobile(), modifyStaff.getAreaId());
        LeadsUser exist = leadsUserRepository.findTopByChildBigNameAndMobileAndAreaIdAndDeletedFalse(leadsUser.getLeadsId(), leadsUser.getMobile(), modifyStaff.getAreaId());
        if(exist != null){
            throw new BusinessException(ErrorCode.LEADS_NOT_EXIST, "已经存在相同联系人");
        }
        leadsUserRepository.save(leadsUser);

        Leads leads = leadsRepository.findById(leadsUser.getLeadsId()).get();
        //添加销售动态 follow
        LeadsHistory leadsHistory = new LeadsHistory();
        if (StringUtils.isBlank(leadsUser.getLeadsId())) {
            Date date = new Date();
            leadsHistory.setStaffName(leads.getStaffName());
            leadsHistory.setType("leads");
            leadsHistory.setContent("在 " + DateUtil.dateToymd(date) + " 添加了名为"+leadsUser.getUserName()+"的联系人");
            leadsHistory.setOperationType(OPERATE_CREATE);
            leadsHistory.setLeadsId(leads.getId());
            leadsHistory.setActualFollowDate(date);
            leadsHistory.setStaffId(modifyStaff.getId());
            leadsHistory.setStaffName(modifyStaff.getRealName());
            leadsHistory.setStaffHeadImgUrl(modifyStaff.getHeadImgUrl());
            leadsHistory.setAreaId(leads.getAreaId());
        }
        if ("leads".equals(leadsHistory.getType())) {
            leadsHistory.setLeadsId(leads.getId());
            leadsHistoryRepository.save(leadsHistory);
        }
        return leadsUser;
    }

    @Override
    public LeadsUser editType(LeadsUser leadsUser, String ticket) {
        StaffDTO modifyStaff = ssoCallService.getStaffDTO(ticket);
        LeadsUser user = leadsUserRepository.findBytLeadsIdAndType(leadsUser.getLeadsId());
        user.setType(leadsUser.getType());
        leadsUserRepository.save(user);
        LeadsUser leadsUser1 = leadsUserRepository.findById(leadsUser.getId()).get();
        leadsUser1.setType(1);
        leadsUserRepository.save(leadsUser1);
        Leads leads = leadsRepository.findById(leadsUser.getLeadsId()).get();


        //添加销售动态 follow
        LeadsHistory leadsHistory = new LeadsHistory();
        if (!StringUtils.isBlank(leadsUser.getLeadsId())) {
            Date date = new Date();
            leadsHistory.setStaffName(leads.getStaffName());
            leadsHistory.setType("leads");
            leadsHistory.setContent("在 " + DateUtil.dateToymd(date) + " 修改"+leadsUser1.getUserName()+"为第一联系人");
            leadsHistory.setOperationType(OPERATE_FOLLOW);
            leadsHistory.setLeadsId(leads.getId());
            leadsHistory.setActualFollowDate(date);
            leadsHistory.setStaffId(modifyStaff.getId());
            leadsHistory.setStaffName(modifyStaff.getRealName());
            leadsHistory.setStaffHeadImgUrl(modifyStaff.getHeadImgUrl());
            leadsHistory.setAreaId(leads.getAreaId());
        }
        if ("leads".equals(leadsHistory.getType())) {
            leadsHistory.setLeadsId(leads.getId());
            leadsHistoryRepository.save(leadsHistory);
        }
        return leadsUser;
    }

    @Override
    public List<LeadsUser> leadsByInfo(StaffDTO staff, LeadsUser leadsUser) {
        List<LeadsUser> list=leadsUserRepository.findByLeadsIdOrderByTypeAsc(leadsUser.getId());
        return list;
    }

    @OptsTrace(code = "30005")
    @Override
    public LeadsUser update(LeadsUser leadsUser, String ticket) {
        StaffDTO modifyStaff = ssoCallService.getStaffDTO(ticket);
        LeadsUser user = leadsUserRepository.findById(leadsUser.getId()).get();
        BeanUtils.copyProperties(leadsUser,user,"id");
        leadsUserRepository.save(user);
        return user;
    }
}
